package org.bjtu.hadooptest.hadoop;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.fs.FileSystem;

import java.io.*;


public class MoveFile {
    /**
     * 移动文件
     */
    public static boolean mv(Configuration conf, String remoteFilePath,
                             String remoteToFilePath) {
        try (FileSystem fs = FileSystem.get(conf)) {
            Path srcPath = new Path(remoteFilePath);
            Path dstPath = new Path(remoteToFilePath);
            return fs.rename(srcPath, dstPath);
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 主函数
     */
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.set("fs.defaultFS", "hdfs://172.31.42.153:9000");
        String remoteFilePath = "/user/root/text.txt"; // 源文件HDFS路径
        String remoteToFilePath = "/user/root/input"; // 目的HDFS路径

        try {
            if (MoveFile.mv(conf, remoteFilePath, remoteToFilePath)) {
                System.out.println("将文件 " + remoteFilePath + " 移动到 "
                        + remoteToFilePath);
            } else {
                System.out.println("操作失败(源文件不存在或移动失败)");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

//    作者：Tiny_16
//    链接：https://www.jianshu.com/p/0663d74b79b5
//    來源：简书
//    著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。
}
